package com.zhongmeng.rxjava2test.retrofit


import com.zhongmeng.rxjava2test.bean.Banner
import com.zhongmeng.rxjava2test.bean.BaseBean
import com.zhongmeng.rxjava2test.bean.LoginBean
import com.zhongmeng.rxjava2test.bean.Notice
import com.zhongmeng.rxjava2test.bean.Work
import com.zhongmeng.rxjava2test.retrofit.ApiServiceManager.Companion.TIME_OUT
import com.zhongmeng.rxjava2test.retrofit.ApiServiceManager.Companion.token
import com.zhongmeng.rxjava2test.utils.SaveUserInfo

import java.io.IOException
import java.util.concurrent.TimeUnit

import io.reactivex.Observable
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory


/**
 * author :rcw.
 * date :2017/10/11 11:53.
 */

//私有构造方法
class ApiServiceManager {
    companion object {
        private val token = SaveUserInfo.instance.token
        private val baseurl = "http://192.168.1.68:8080/cdc_appInterface/"
        private val TIME_OUT = 10

        //private val apiServiceManager: ApiServiceManager? = null

        //创建retrofit对象实例
        private val retrofit = Retrofit.Builder()
                .baseUrl(baseurl)
                .addConverterFactory(GsonConverterFactory.create())//将返回的数据转为Gson
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//添加Rxjava的回调
                .client(genericClient())
                .build()
        //通过retrofit获取ApiService实例
        private val apiService = retrofit.create(ApiService::class.java)

        fun genericClient(): OkHttpClient {
            return OkHttpClient.Builder()
                    .connectTimeout(TIME_OUT.toLong(), TimeUnit.SECONDS)
                    .addInterceptor { chain ->
                        val request = chain.request()
                                .newBuilder()
                                .addHeader("token", token)
                                .build()
                        chain.proceed(request)
                    }
                    .build()
        }
        /*//单例模式获取实例
    public static ApiServiceManager getInstance() {
        if (apiServiceManager == null) {
            synchronized (ApiServiceManager.class) {
                if (apiServiceManager == null) {
                    apiServiceManager = new ApiServiceManager();
                }
            }
        }
        return apiServiceManager;
    }*/

        /**
         * 登录
         *
         * @param cid      手机cid
         * @param username
         * @param password
         * @return 返回类型为Observable型的实体类
         */
        fun getCallBackData(cid: String, username: String, password: String): Observable<LoginBean> {
            return apiService.getLoginInfo(cid, username, password)
        }

        val bannerUrl: Observable<Banner>
            get() = apiService.bannerUrl

        fun getNoticeInfo(types: String, page: String, pagesize: String, areacode: String): Observable<Notice> {
            return apiService.getNoticeInfo(types, page, pagesize, areacode)
        }

        fun getCdcWorkInfo(unitcode: String): Observable<Work> {
            return apiService.getCdcWorkInfo(unitcode)
        }

        fun getWorkInfo(areacode: String): Observable<Work> {
            return apiService.getWorkInfo(areacode)
        }

        val qiuniuToken: Observable<BaseBean>
            get() = apiService.qiuniuToken
    }


}